﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TLM.Wizard.ViewModels
{
    public class ConversionChoiceViewModel : BaseViewModel
    {        
        private bool _isConvertingToPlaylist;
        private string _playlistConversionResultDestinationPath;

        private bool _isConvertingToTestCategory;
        private string _testCategoryConversionResultDestinationPath;

        public bool IsConvertingToPlaylist
        {
            get
            {
                return _isConvertingToPlaylist;
            }
            set
            {
                if (_isConvertingToPlaylist != value)
                {
                    _isConvertingToPlaylist = value;
                    RaisePropertyChanged("IsConvertingToPlaylist");
                }
            }
        }
        
        
        public string PlaylistConversionResultDestinationPath
        {
            get
            { 
                return _playlistConversionResultDestinationPath; 
            }
            set
            {
                if (_playlistConversionResultDestinationPath != value)
                {
                    _playlistConversionResultDestinationPath = value;
                    RaisePropertyChanged("PlaylistConversionResultDestinationPath");
                }
            }
        }

        
        public string TestCategoryConversionResultDestinationPath
        {
            get
            {
                return _testCategoryConversionResultDestinationPath;
            }
            set
            {
                if (_testCategoryConversionResultDestinationPath != value)
                {
                    _testCategoryConversionResultDestinationPath = value;
                    RaisePropertyChanged("TestCategoryConversionResultDestinationPath");
                }
            }
        }

        
        public bool IsConvertingToTestCategory
        {
            get
            {
                return _isConvertingToTestCategory;
            }
            set
            {
                if (_isConvertingToTestCategory != value)
                {
                    _isConvertingToTestCategory = value;
                    RaisePropertyChanged("IsConvertingToTestCategory");
                }
            }
        }

        public RelayCommand PickPlaylistPathCommand { get; set; }
        public RelayCommand PickTestCategoryPathCommand { get; set; }
        private bool isPickingPath = false;
     
        public ConversionChoiceViewModel()
        {
            this.PickPlaylistPathCommand = new RelayCommand(() =>
            {
                isPickingPath = true;
                this.PickPath((path) =>
                {
                    this.PlaylistConversionResultDestinationPath = path;
                    isPickingPath = false;
                });
            }, () => { return !isPickingPath; });

            this.PickTestCategoryPathCommand = new RelayCommand(() =>
            {
                isPickingPath = true;
                this.PickPath((path) =>
                {
                    this.TestCategoryConversionResultDestinationPath = path;
                    isPickingPath = false;
                });
            }, () => { return !isPickingPath; });
        }

        private void PickPath(Action<string> passPathAction)
        {
            // The user can select a folder for the playlists to be saved
            using (FolderBrowserDialog saveDialog = new FolderBrowserDialog())
            {
                saveDialog.ShowNewFolderButton = true;
                saveDialog.SelectedPath = Environment.SpecialFolder.Personal.ToString();
                saveDialog.Description = "Select a folder to store the playlists";
                DialogResult result = saveDialog.ShowDialog();

                if (result == DialogResult.OK)
                {
                    string path = saveDialog.SelectedPath;
                    if (passPathAction != null)
                    {
                        passPathAction(path);
                    }
                }
            }
        }


        public override void PrepareNavigation()
        {
            Messages.ChoiceMessage msg = new Messages.ChoiceMessage()
            {
                MigrateToPlaylist = _isConvertingToPlaylist,
                MigrateToTestCategory = IsConvertingToTestCategory,
                PlaylistPath = _playlistConversionResultDestinationPath,
            };

            Messenger.Default.Send<Messages.ChoiceMessage>(msg);
        }
    }
}
